package com.edais.service.imp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.edais.dao.RiskEvaluationDao;
import com.edais.dao.SystemSettingDao;
import com.edais.domain.RiskEvaluation;
import com.edais.service.RiskEvaluationService;
import com.edais.util.StringUtil;


/**
 * 风险评估
 * @author gaojing
 *
 */
@Service
public class RiskEvaluationServiceImpl implements RiskEvaluationService{

	private static final Log logger = LogFactory.getLog(RiskEvaluationServiceImpl.class);
	
	@Autowired
	private RiskEvaluationDao riskEvaluationDao;
	@Autowired
	private SystemSettingDao systemSettingDao;
/*	@Autowired
	private LdapService ldapService;*/
	
	/**
	 * 风险承受能力评估
	 * @param paraMap
	 * @return
	 * @throws Exception
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public Map<String, Object> queryRiskEvaluation(Map<String, String> paraMap) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String riskType = "";
		String declare = "";
		String userId = paraMap.get("userId");
		String answer = paraMap.get("answer");
		String markType =systemSettingDao.getSystemSettingValueByName("risk_evaluation_type");
		//String markType =ldapService.getSystemSetting("risk_evaluation_type").getValue();

		logger.info(userId+"查询答案对于的分数：" + markType);
		
		List list = StringUtil.stringToArray(markType);
		
		String[] a1 = (String[]) list.get(0);
		String[] a2 = (String[]) list.get(1);
		Map<String, Integer> map = new HashMap<String, Integer>();
		int i = 0;
		while (i < a1.length) {
			map.put(a1[i], Integer.valueOf(a2[i]));
			i++;
		}
		
		String[] temp = answer.split("\\|");
		int mark = 0;
		for (int j = 0; j < temp.length; j++) {
			mark += map.get(temp[j]);
		}
		logger.info(userId+" 用户风险评估的分数：" + mark);
		
		List<Map<String, String>> listMap = riskEvaluationDao.queryParamValue("risk_evaluation_info");
		
		for (int j = 0; j < listMap.size(); j++) {
			Map<String, String> map2 = listMap.get(j);
			String value = (String) map2.get("value");
			String[] tmp = value.split("\\|");
			String first =tmp[0];
			String last =tmp[1];
			
			if (mark >= Integer.valueOf(first) && mark <= Integer.valueOf(last)) {
				riskType = tmp[2];
				declare = map2.get("htmlInfo");
				continue;	
			} 
		}

		/*SystemSetting systemSetting = new SystemSetting();
		for (int j = 1; j < 6; j++) {
			systemSetting = ldapService.getSystemSetting("risk_evaluation_info"+j);
			String value = systemSetting.getValue();
			String[] tmp = value.split("\\|");
			String first =tmp[0];
			String last =tmp[1];
			if (mark >= Integer.valueOf(first) && mark <= Integer.valueOf(last)) {
				riskType = tmp[2];
				declare = systemSetting.getHtmlInfo();
				continue;
			}
		}*/

		RiskEvaluation risk = new RiskEvaluation();
		risk.setUserId(Integer.valueOf(userId));
		risk.setIsRisk("Y");
		risk.setMark(mark);
		risk.setRiskType(riskType);
		risk.setDeclare(declare);
		int count = riskEvaluationDao.updateOrInsert(risk);
		
		logger.info(userId +" 用户更新插入风险评估信息：" + count+ "条");

		resultMap.put("riskType", riskType);
		return resultMap;
		
	}
	
	/**
	 * 通过userId查询用户是否进行风险评估
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public RiskEvaluation getRiskEvaluationByUserId(String userId) throws Exception{
		RiskEvaluation risk = riskEvaluationDao.selectRiskEvaluationByUserId(Integer.parseInt(userId));
		if(risk == null){
			risk =new RiskEvaluation();
			risk.setIsRisk("N");
			risk.setRiskType("未评估");
		}

		return risk;
	}
   
}